/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;

/**
 *
 * @author Do Gia Cuong
 */

import java.io.*;
import java.util.ArrayList;
import java.util.Map;


public class Debug {

    private static final boolean PRINT_TO_FILE = true;
    private static final String LOG_FILENAME = "log_transaction.txt";
    private static final String LOG_FILENAME_FRAUDCLICK = "faud_click.txt";
    private static OutputStreamWriter stream = null;

    public static void println(String s)
    {
        System.out.println(TimeUtil.getCurrentTimeInStringForLog() +  "  Debug:" +  s);
        writeToLogFile(TimeUtil.getCurrentTimeInStringForLog() + "  Debug: " + s + "\r\n");
    }
    
    public static void printlnFaudClick(String s)
    {
        System.out.println(TimeUtil.getCurrentTimeInStringForLog() +  "  Debug:" +  s);
        writeToLogFileFraudClick(TimeUtil.getCurrentTimeInStringForLog() + "  Debug: " + s + "\r\n");
    }

    public static void print(String s) {
        System.out.println(TimeUtil.getCurrentTimeInString() + "Debug: " + s);
        writeToLogFile(TimeUtil.getCurrentTimeInString() + "Debug: " + s);
    }
     
    public static void writeToLogFileFraudClick(String sData)
    {
        if (!PRINT_TO_FILE) return;

        try
        {
            if (stream == null)
            {
                String fullPath = getCurrentDir() + LOG_FILENAME_FRAUDCLICK;
                System.out.println("Debug: full path of log file = " + fullPath);
                File file = new File(fullPath);
                if(!file.exists())
                {
                    file.createNewFile();
                }
                stream = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            }
            if (stream != null)
            {
                if (sData == null || sData.length() == 0)
                {
                    return;
                }
                stream.append(sData);
                stream.flush();
                //stream.close();
            }
        }
        catch (Exception e)
        {
            System.out.println("Error in writeToLogFile : " + e);
        }
    }

     public static void writeToLogFile(String sData)
    {
        if (!PRINT_TO_FILE) return;

        try
        {
            if (stream == null)
            {
                String fullPath = getCurrentDir() + LOG_FILENAME;
                System.out.println("Debug: full path of log file = " + fullPath);
                File file = new File(fullPath);
                if(!file.exists())
                {
                    file.createNewFile();
                }
                stream = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            }
            if (stream != null)
            {
                if (sData == null || sData.length() == 0)
                {
                    return;
                }
                stream.append(sData);
                stream.flush();
                //stream.close();
            }
        }
        catch (Exception e)
        {
            System.out.println("Error in writeToLogFile : " + e);
        }
    }

     public static void closeLogFile()
    {
        if (!PRINT_TO_FILE) return;

        if (stream != null)
        {
            try
            {
                stream.close();
            }
            catch (Exception e)
            {
                System.out.println("writeLog : " + e);
            }
        }
    }

    public static String getCurrentDir()
    {
        String currentDir = new File(".").getAbsolutePath();
        currentDir = currentDir.replace("\\", "/");
        if (currentDir.endsWith(".")) {
            currentDir = currentDir.substring(0, currentDir.length() - 1);
        }
        return currentDir;
    }

   

}
